package com.example.simple.mall.service.impl;

import com.example.simple.mall.bean.User;
import com.example.simple.mall.common.ServerResponse;
import com.example.simple.mall.dao.UserDao;
import com.example.simple.mall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/6/19
 */
@Service
public class IUserServiceImpl implements IUserService {
    private final UserDao userDao;

    @Autowired
    public IUserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public ServerResponse<User> selectelogin(String username, String password) {
        int userCount = userDao.checkedUsername(username);
        if (userCount == 0) {
            return ServerResponse.createByErrorMessage("用户不存在");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        User user = userDao.selectelogin(username, password);
        if (user == null) {
            return ServerResponse.createByErrorMessage("密码错误");
        }
        if (user.getJingyong() == 1) {
            return ServerResponse.createByErrorMessage("用户已被禁用，用户可重新启用");
        }
        user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);
        return ServerResponse.createBySuccessMessage("成功登陆", user);
    }

    @Override
    public ServerResponse<User> reboot(String username, String password) {
        int userCount = userDao.checkedUsername(username);
        if (userCount == 0) {
            return ServerResponse.createByErrorMessage("用户不存在");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        User user = userDao.selectelogin(username, password);
        if (user == null) {
            return ServerResponse.createByErrorMessage("密码错误");
        }
        if (user.getJingyong() == 1) {
            user.setUsername(userDao.rebootJingyong(username));
            return ServerResponse.createByErrorMessage("用户取消禁用成功");
        }
        if (user.getJingyong() == 0) {
            return ServerResponse.createByErrorMessage("用户未被禁用");
        }
        return ServerResponse.createByErrorMessage("未知错误");
    }

    @Override
   public ServerResponse<User> passwordReset(String email){
       int emailCount = userDao.checkedEmail(email);
       if (emailCount==0) {
           return ServerResponse.createByErrorMessage("该邮箱不存在");
       }
//       String url = "http://192.168.43.182/password_reset"+RandomStringUtils.random(20,"abc");
//       User user = new User();
//       user.setCode(url);
       return null;
   }

    @Override
    public ServerResponse<User> registered(User user) {
        int userCount = userDao.checkedUsername(user.getUsername());
        if (userCount > 0) {
            return ServerResponse.createByErrorMessage("用户已存在");
        }
        int emailCount = userDao.checkedEmail(user.getEmail());
        if (emailCount > 0) {
            return ServerResponse.createByErrorMessage("该邮箱已存在");
        }
        String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Password);
        return ServerResponse.createBySuccessMessage("注册成功", user);
    }
}
